package com.example.onlinemonitoring.comCenter;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Imsensor;
import com.example.onlinemonitoring.serviceImpl.ImsensorServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service("im61850Com")
public class Im61850Com extends CommPortManager {
    @Autowired
    private ImsensorServiceImpl imserviceimpl;

    @Override
    public void receiveData(String str) {
        setText("收到sanway请求：" + str + "\r\n");
        int startAddr = parseHex4(str.substring(0, 2));
        String flag = str.substring(6, 8);
        String s = "";
        Imsensor im = new Imsensor();
        im.setStationid(ad.getStationid());
        im.setDevicegroup(ad.getDevicegroup());
        im.setDeviceaddr(startAddr);
        if (flag.equals("34")) {
            im.setPointaddr(1);
            im = imserviceimpl.getModel(im);
            if (im == null)
                im = new Imsensor();
            s += str.substring(0, 2);
            s += "0404";
            s += IEEE754Utils.singleToHexConvert(isnull(im.getLastdata()));
            s += CRC16.calcCrc16(s);
            Send(s);
            setText("SANWAY模块转发铁芯数据：" + s + "\r\n");
        } else if (flag.equals("36")) {
            im.setPointaddr(2);
            im = imserviceimpl.getModel(im);
            if (im == null)
                im = new Imsensor();
            s += str.substring(0, 2);
            s += "0404";
            s += IEEE754Utils.singleToHexConvert(isnull(im.getLastdata()));
            s += CRC16.calcCrc16(s);
            Send(s);
            setText("SANWAY模块转发夹件数据：" + s + "\r\n");
        }
    }
}
